<template>
  <div class="goods">
    <!-- 搜索区域 -->
    <div class="header">
      <el-input
        v-model="input"
        placeholder="请输入内容"
        @change="searchInput"
      ></el-input>
      <el-button type="primary">查询</el-button>
      <el-button type="primary">
        <router-link to="/layout/addgoods" style="color: #fff"
          >页面添加</router-link
        >
      </el-button>
      <el-button type="primary" @click="addGoods" style="color: #fff"
        >弹窗添加</el-button
      >
    </div>
    <!-- 表格展示区域 -->
    <div class="wrapper">
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
        @row-click="handleRowClick"
        ref="handSelectTest_multipleTable"
        row-key="id"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="商品ID" width="100">
        </el-table-column>
        <el-table-column
          prop="title"
          label="商品名称"
          width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="price" label="商品价格" width="100">
        </el-table-column>
        <el-table-column prop="num" label="商品数量" width="100">
        </el-table-column>
        <el-table-column
          prop="category"
          label="规格类目"
          width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="image" label="商品图片" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="sellPoint"
          label="商品卖点"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column prop="descs" label="商品描述" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-search">查看</el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              type="primary"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              icon="el-icon-delete"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-button
        type="danger"
        icon="el-icon-delete"
        @click="delAll"
        :disabled="multipleSelection.length === 0"
        style="margin: 15px"
      >
        一键删除
      </el-button>
    </div>
    <!-- 分页 -->
    <Mypagination
      :total="total"
      :pageSize="pageSize"
      @changePage="changePage"
      :current-page="currentPage"
    />

    <!-- <GoodsDialog :dialogVisible='dialogVisible' @close='close'/> -->
    <GoodsDialog ref="dialog" :editOrAdd="editOrAdd" :rowData="rowData" />
  </div>
</template>

<script>
import Mypagination from "@/components/Mypagination.vue";
import GoodsDialog from "./GoodsDialog.vue";
import { MessageBox } from "element-ui";
export default {
  data() {
    return {
      input: "",
      tableData: [],
      total: 10,
      pageSize: 1,
      type: 1,
      list: [],
      dialogVisible: false,
      currentPage: 1,
      editOrAdd: "添加商品",
      rowData: {},
      multipleSelection: [],
    };
  },
  methods: {
    // 批量删除
    //点击复选框触发，复选框样式的改变
    handleSelectionChange(val) {
      console.log(val); //打印选中的行集合
      this.multipleSelection = val;
    },
    //点击行触发，选中或不选中复选框
    handleRowClick(row, column, event) {
      this.$refs.handSelectTest_multipleTable.toggleRowSelection(row);
      console.log(row); //打印的时当前选中的行
    },
    //点击批量删除
    delAll() {
      var arr = []; //
      //遍历点击选择的对象集合，拿到每一个对象的id添加到新的集合中
      this.multipleSelection.forEach(row => arr.push(row.id));

      console.log(
        "id",
        this.multipleSelection.forEach(row => arr.push(row.id))
      );
      this.$confirm("确定删除吗", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "success",
        callback: action => {
          if (action === "confirm") {
            //批量删除
            this.$api
              .bathDelete({
                id: arr,
              })
              .then(res => {
                console.log(res.data);
                this.$notify({
                  title: "删除成功",
                  message: "",
                  type: "success",
                  duration: 2000,
                });
                //重新加载页面的方法
                this.http(1);
              })
              .catch(() => {
                console.log("error submit");
              });
          }
        },
      });
    },
    close() {
      this.dialogVisible = false;
    },
    //  添加商品
    addGoods() {
      this.editOrAdd = "添加商品";
      // this.dialogVisible = true
      this.$refs.dialog.dialogVisible = true;
    },
    // 搜索
    searchInput(val) {
      // 没有数据页码都是1
      if (!val) {
        this.http(1);
        return;
      }
      console.log(val);
      this.$api
        .getSearch({
          search: val,
        })
        .then(res => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.currentPage = 1;
            this.list = res.data.result;
            this.total = res.data.result.length;
            this.pageSize = 3; // 每页显示的条数
            this.tableData = res.data.result.slice(0, 3);
            this.type = 2; //
          } else {
            this.tableData = [];
            // this.type=1
            this.total = 1;
            this.pageSize = 1;
            this.type = 1;
          }
        });
    },

    // 编辑
    handleEdit(index, row) {
      console.log(index, row);
      this.editOrAdd = "编辑商品";
      // 打开弹窗
      this.$refs.dialog.dialogVisible = true;

      // 传递数据
      this.rowData = { ...row };
    },
    // 删除
    handleDelete(index, row) {
      console.log(index, row);
      MessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api
            .delGoods({
              id: row.id,
            })
            .then(res => {
              console.log(res.data);
              if (res.data.status === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.http(1);
              } else {
                this.$message({
                  type: "error",
                  message: "删除失败!",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 改变页码
    changePage(page) {
      this.currentPage = page; // 改变当前页码
      this.http(page);
      if (this.type === 1) {
        this.http(page);
      } else {
        this.tableData = this.list.slice((num - 1) * 3, num * 3);
      }
    },
    http(page) {
      this.$api
        .getGoodsList({
          page,
        })
        .then(res => {
          console.log(res.data);
          if (res.data.status === 200) {
            this.tableData = res.data.data;
            this.total = res.data.total;
            this.pageSize = res.data.pageSize;
          }
        });
    },
  },
  mounted() {
    this.http(1);
  },
  components: { Mypagination, GoodsDialog },
};
</script>

<style lang="scss" scoped>
.goods {
  margin: 20px;
  .header {
    display: flex;
    button {
      margin-left: 20px;
    }
  }
}
.wrapper {
  margin: 20px 0;
}
</style>
